//递归查找符合条件的对象
export const getOrgIdArr = (data, val) => {
  for (const element of data) {
    if (element.id == val) {
      return element;
    }
    if (element.children) {
      return this.getOrgIdArr(element.children, val);
    }
  }
}

//递归获取所有子元素
export const recurrence = item => {
  let list = [], children = [];
  item.children.map((val) => {
    if (item.id == val.parentId) {
      list.push({
        ...val
      });
    }
  });
  let arr = [];
  list.map((s) => {
    if (s.hasOwnProperty('children')) {
      arr = this.recurrence(s);
      children.push({
        ...s,
        children: arr
      });
    } else {
      children.push({
        ...s
      });
    }
  });
  return children;
};

//根据传入的城市数据找出某一对应城市
export const getArea = (json, code) => {
  let names = [];
  let codes = [];
  function findCity(list, code, father) {
    const index = list.findIndex(ev => ev.code == code);
    if (index > -1) {
      const child = list[index];
      names.unshift(child.name && child.name);
      codes.unshift(child.code);
      father && names.unshift(father.name && father.name) && codes.unshift(father.code);
      father && findCity(json || [], father.code);
      return;
    }
    list.map(item => {
      if (item.childrens) {
        findCity(item.childrens || [], code, item);
      }
    })
  }
  findCity(json, code);
  names = [...new Set(names)];
  codes = [...new Set(codes)];
  return { codes, names };
};
